package com.wr.online.test.controller;

import com.wr.online.test.exception.GlobalException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpSession;
import java.io.File;

import java.io.IOException;

import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    public static final int File_MAX_SIZE=10*1024*1024;//上传限制是1兆

    @PostMapping("/submit/{id}")
    public void subFile(@RequestParam("file")MultipartFile file, HttpSession session, @PathVariable Integer rid,@PathVariable Integer uid){
        if(file.isEmpty()||file.getSize()>File_MAX_SIZE){
            throw new GlobalException();
        }
        String parent=session.getServletContext().getRealPath("upload");
        File dir=new File(parent);
        if(!dir.exists()){
            dir.mkdirs();
        }
        String originalFilename=file.getOriginalFilename();//原始名称
        int beginIndex=originalFilename.lastIndexOf(".");
        String suffix=originalFilename.substring(beginIndex);//后缀
        String filename= UUID.randomUUID().toString().toUpperCase()+suffix;//生成随机名称
        File dest=new File(dir,filename);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String resume="/upload/"+filename;
//        service.subFile(rid,resume);
    }
}